<?php

namespace Admin\Controller;
use Think\Controller;

class ProductController extends CommonController {
    public function index(){
    	$this->display('index');
    }
    
    
    public function add(){
    	if(IS_POST){
        	//判断分类是否为空
	    	if($_POST['pclassid']==NULL||$_POST['pclassid']==0){
	    		$this->error("大分类不能为空");
	    	}else if ($_POST['psubclassid']==NULL||$_POST['psubclassid']==0){
	    		$this->error("子分类不能为空");
	    	}else{
	    		$config = array(
			        'maxSize'       =>  3145728 , //上传的文件大小限制 (0-不做限制)
			        'exts'          =>  array('jpg', 'gif', 'png', 'jpeg'), //允许上传的文件后缀
			        'savePath'      =>  './Uploads/product/', //保存路径
		        	'autoSub'       =>  false,
		        	'rootPath'      =>  './Public/', //保存根路径
			        'saveName'      =>  array('uniqid', ''), //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
			    );
			    $upload= new \Think\Upload($config);
		    	$info = $upload->uploadOne($_FILES['pic']);;
		    	if(!$info){
		    		$this->error('图片上传失败！失败原因:'.$info->error);
		    	}else{
		    		$_POST['pic']=$info['savepath'].$info['savename'];
		    		$data=M('Product');
		    		if($data->create()){
		    			if($data->add()){
		    				$this->success("添加成功！");
		    			}else{
		    				$this->error("添加失败！错误代码：001","",5);
		    			}
		    		}else{
		    			$this->error("添加失败！错误代码：001","",5);
		    		}
		    	}
	    	}
    	}else{
    		$this->error("非法请求！");
    	}
    
    }
    
    public function ajaxpage(){
    	$p = M('product'); 
		$count = $p->count();
		$Page = new \Think\Ajaxpage($count,2);
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','尾页');
		$list = $p->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
		$show = $Page->show();// 分页显示输出
		$arr=array(
			"list"=>$list,
			"show"=>$show
		);
		$this->ajaxReturn($arr);
    }

}